package com.s3u4.demo.zookeeper.lock;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Captain on 01/12/2017.
 */
public class Client {

    public static int index = 0;

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ExecutorService executor = Executors.newFixedThreadPool(64);

        for ( int i = 0 ; i < 10 ; i++ ){
            executor.execute( () -> {
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ZkLock lock = new ZkLock();
                try {
                    lock.lock();
                    for(int j = 0 ; j < 1000 ; j++){
                        index++;
                    }
                } finally {
                    lock.unLock();
                }
            });
        }

        countDownLatch.countDown();
        executor.shutdown();
        Thread.sleep(2000);
        System.out.println("index --> " + index);

    }
}
